﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;


using System.Data.SqlClient;

using System.Xml;
using System.Xml.XPath;
using XunPan.Web.Globals;
using SalePartial.Utility;

namespace DataLayerAgent
{



	public class SiteMapGlobals
	{

		//Private Sub UpdateSiteMap()

		//    Dim ojbXMLDocument As New XmlDocument
		//    ojbXMLDocument.Load(HttpContext.Current.Server.MapPath("~/Web.sitemap"))

		//    CreateMenuXmlFile(0, ojbXMLDocument)

		//    ojbXMLDocument.Save(HttpContext.Current.Server.MapPath("~/Web.sitemap"))

		//End Sub





		//'创建   xml树的方法   


		public static void CreateWareCubeSiteMapXmlDataFile(ref XmlDataDocument xmlDom)
		{

			XmlAttribute nodeAttr = null;
			//Dim rootNode As XmlNode
			XmlNode childNode = null;

			//*********************************************   
			//建立根节点   —— <siteMap>
			//*********************************************   
			//rootNode = xmlDom.AppendChild(xmlDom.CreateElement("siteMap"))

			//*********************************************   
			//建立第一级节点   —— <siteMapNode>  '直接将零部件库作为根节点 2010-04-17
			//*********************************************   
			//childNode = xmlDom.CreateElement("siteMapNode")
			childNode = xmlDom.AppendChild(xmlDom.CreateElement("siteMapNode"));

			//节点指示页面的URL —— 标准件类别的URL
			nodeAttr = xmlDom.CreateAttribute("url");
			nodeAttr.Value = "~/WareCube/partlist.aspx";
			childNode.Attributes.Append(nodeAttr);


			//节点名称 —— 标准件类别名称
			nodeAttr = xmlDom.CreateAttribute("title");
			nodeAttr.Value = "-1";
			childNode.Attributes.Append(nodeAttr);


			//节点说明/描述 —— 标准件类别说明/描述
			nodeAttr = xmlDom.CreateAttribute("description");
			nodeAttr.Value = "零部件库";
			childNode.Attributes.Append(nodeAttr);

			//节点指示页面的URL —— 标准件类别的ID
			nodeAttr = xmlDom.CreateAttribute("key");
			nodeAttr.Value = "00000000-0000-0000-0000-000000000000";
			childNode.Attributes.Append(nodeAttr);


			//rootNode.AppendChild(childNode)


			//++++++++++++++++++++++++++   
			//从数据据获取数据   
			//++++++++++++++++++++++++++ 
            SqlConnection conPortal = new SqlConnection(GlobalSetting.ConnectionString);
			SqlCommand cmd = new SqlCommand("XunPan_WareCubeGetFirstLevelChildrenFromID", conPortal);

			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Parameters.Add("categoryID", SqlDbType.UniqueIdentifier).Value = new Guid("00000000-0000-0000-0000-000000000000");

			SqlDataAdapter adp = new SqlDataAdapter(cmd);


			DataTable dtable = new DataTable("CategorySiteMap");
			dtable.Columns.Add("XmlLevel", System.Type.GetType("System.Int32")).DefaultValue = 1;
			adp.Fill(dtable);

			DataColumn colCount = dtable.Columns["parentID"];


			int lvl = 1;

			DataTable tempTable = new DataTable("TempCategoryList");
			tempTable.Columns.Add("XmlLevel", System.Type.GetType("System.Int32")).DefaultValue = 0;


			do {
				foreach (DataRow drow in dtable.Rows) {
					if (drow["isParent"].ToString()== "true" && Convert.ToInt32( drow[0].ToString()) == lvl) {
						cmd.Parameters["categoryID"].Value = drow["categoryID"];
						adp.Fill(tempTable);
					}
				}

				foreach (DataRow dr in tempTable.Rows) {
					dr[0] = lvl + 1;
				}

				if (tempTable.Rows.Count == 0) {
					break; // TODO: might not be correct. Was : Exit Do
				} else {
					dtable.Merge(tempTable);

					tempTable.Rows.Clear();
					lvl = lvl + 1;
				}
			} while (true);


			string _ID = null;
			string _Name = string.Empty;
			string _parentID = null;



			for (int i = 0; i <= dtable.Rows.Count - 1; i++) {
				_ID = dtable.Rows[i]["categoryID"].ToString();
				_Name = Strings.Trim(Convert.ToString(dtable.Rows[i]["name"].ToString()));
				_parentID = dtable.Rows[i]["parentID"].ToString();



				childNode = xmlDom.CreateElement("siteMapNode");
				//添加子节点属性   

				//'节点指示页面的URL —— 标准件类别的URL
				//nodeAttr = xmlDom.CreateAttribute("url")
				//nodeAttr.Value = "~/StandardPart/CategoriesList.aspx?ID=" & _ID.ToString
				//childNode.Attributes.Append(nodeAttr)

				//节点指示页面的URL —— 标准件类别的URL
				nodeAttr = xmlDom.CreateAttribute("url");
				nodeAttr.Value = "~/part.aspx?c=" + _ID.ToString();
				childNode.Attributes.Append(nodeAttr);


				//节点名称 —— 标准件类别名称
				nodeAttr = xmlDom.CreateAttribute("title");
				nodeAttr.Value = _ID.ToString();
				childNode.Attributes.Append(nodeAttr);


				//节点说明/描述 —— 标准件类别说明/描述
				nodeAttr = xmlDom.CreateAttribute("description");
				nodeAttr.Value = _Name;
				childNode.Attributes.Append(nodeAttr);



				//节点指示页面的URL —— 标准件类别的ID
				nodeAttr = xmlDom.CreateAttribute("key");
				nodeAttr.Value = _ID.ToString();
				childNode.Attributes.Append(nodeAttr);

				//增加子节点
				InsertMapNodeToDataXML(ref childNode, xmlDom, _parentID);

			}

			if (!(conPortal.State == ConnectionState.Closed)) {
				conPortal.Close();
			}
			//返回xmlDom   

		}



		//通过ParentID来查询父节点，并将其作为子节点添加

		public static void InsertMapNodeToDataXML(ref XmlNode xmlChildNode, XmlDataDocument xmlDataDoc, string cateParentID)
		{
			XmlNode rootNode = SelectMapNodeByCateID(ref xmlDataDoc, cateParentID);
			//添加子节点sitmap   
			if (xmlChildNode == null || rootNode == null) {

			} else {
				rootNode.AppendChild(xmlChildNode);
			}

		}

		//通过ID寻找节点，如果为-1则返回根节点，< 0 则返回nothing
		public static XmlNode SelectMapNodeByCateID(ref XmlDataDocument xmlDom, string parentID)
		{

			XmlNode matchNode = null;
			string pathQ = string.Format("//siteMapNode[@key={0}]", "'" + parentID + "'");
			matchNode = xmlDom.SelectSingleNode(pathQ);

			//If parentID = "00000000-0000-0000-0000-000000000000" Then

			//    matchNode = xmlDom.SelectSingleNode("/*/siteMapNode")
			//Else
			//    Dim pathQ As String = String.Format("//siteMapNode[@key={0}]", "'" & parentID & "'")
			//    matchNode = xmlDom.SelectSingleNode(pathQ)


			//End If

			return matchNode;

		}


	}

}
